<template>
  <div>
    <div class="content">
      <div class="content-title">
        <el-button @click="addinfo">返回</el-button>
        <el-button type="primary" @click="subaddinfo">提交</el-button>
        <div></div>
      </div>
      <div class="editinfo">
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="小区名称" prop="communityName">
              <el-input
                v-model="ruleForm.communityName"
              ></el-input> </el-form-item
          ></el-form>
        </div>
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="楼号" prop="buildNo">
              <el-input v-model="ruleForm.buildNo"></el-input> </el-form-item
          ></el-form>
        </div>
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="室号" prop="roomNo">
              <el-input v-model="ruleForm.roomNo"></el-input> </el-form-item
          ></el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "editbuilding",
  data() {
    return {
      ruleForm: {
        communityName: "",
        buildNo: "",
        roomNo: "",
      },
      rules: {
        communityName: [
          { required: true, message: "请输入小区名称", trigger: "blur" },
        ],
        buildNo: [
          { required: true, message: "请输入楼号名称", trigger: "blur" },
        ],
        roomNo: [
          { required: true, message: "请输入室号名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.ruleForm = JSON.parse(this.$route.query.info);
  },
  mounted() {},
  methods: {
    addinfo() {
      this.$router.push({ name: "buildingList" });
    },
    subaddinfo() {
      if (
        this.ruleForm.communityName &&
        this.ruleForm.buildNo &&
        this.ruleForm.roomNo
      ) {
        console.log("发送请求");
        this.ajaxData();
      } else {
        this.$message.error("情输入信息");
      }
    },
    async ajaxData() {
      var params = new FormData();
      params.append("communityName", this.ruleForm.communityName);
      params.append("buildNo", this.ruleForm.buildNo);
      params.append("roomNo", this.ruleForm.roomNo);
      if (this.$route.query.type == "addinfo") {
        // params.append("parentId", 1);
        let { data } = await this.$ajax.lu.addBuildingInfo(params);
        console.log(data);
      } else {
        params.id = this.ruleForm.id;
        params.append("id", this.ruleForm.id);
        let { data } = await this.$ajax.lu.updateBuildingInfo(params);
        console.log(data);
      }
        this.$router.push({ name: "buildingList" });
    },
  },
};
</script>

<style scoped>
.content {
  padding: 50px 170px 20px;
}
.content-title {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-bottom: 20px;
}
.editinfo {
  display: flex;
  justify-content: space-between;
}
</style>
